package com.ceo.auth.jwt.handler;

import cn.hutool.core.util.StrUtil;
import com.ceo.auth.annotaion.Clear;
import com.ceo.auth.jwt.chain.CallableChain;
import com.ceo.auth.jwt.wrapper.JwtWrapper;
import com.ceo.common.threadlocal.ThreadEnvUtil;
import com.ceo.common.vo.UserVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

import java.util.function.Supplier;


/**
 * @Create by hefy
 * @Date 2022/1/10 11:01
 * 身份前置验证(是否跳过验证)
 */
@Component
public class JwtPreAuthenticationHandler extends AbstractJwtHandler<JwtWrapper> {


    @Override
    public Boolean doHandle(CallableChain<Boolean, JwtWrapper> chain, Supplier<JwtWrapper> supplier) throws Exception {

        HandlerMethod handlerMethod = supplier.get().getHandler();

        boolean skipTokenCheck = handlerMethod.getMethod().isAnnotationPresent(Clear.class)
                || handlerMethod.getMethod().getDeclaringClass().isAnnotationPresent(Clear.class);

        //跳过验证，直接成功
        if (skipTokenCheck) {
//            ThreadEnvUtil.SetUserObject(UserVo.getDefaultUser());
            return Boolean.TRUE;
        }
        return chain.doProceed(supplier);
    }

}
